//
//  MyViewController.m
//  ZhongWangYuan
//
//  Created by ZJ on 2018/6/2.
//  Copyright © 2018年 ZJ. All rights reserved.
//

#import "MyViewController.h"
#import "ZJWebViewController.h"
#import "YBMySettingViewController.h"
#import "YBChangePassWordViewController.h"
#import "LPZWYMYHeaderTableViewCell.h"
#import "LPZWYmMyTableViewCell.h"
#import "LPZWYMyDetailModel.h"
#import "TenGrayShadowView.h"
/**  我的收藏  */
#import "LPZWYMyCollectionViewController.h"
/**  我的专题  */
#import "ZJMySpecilViewController.h"
/**  我的课程  */
#import "ZJMyClassViewController.h"
/**  我的考试  */
#import "LPZWYMyTestViewController.h"
#import "MyHeaderStudyAndClassViewController.h"
#import "YBLoginViewController.h"
#define CELLID @"LPZWYmMyTableViewCell"
@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource,LPZWYmMyTableViewCellDelegate,LPZWYMYHeaderTableViewCellDelegate>
/**  collectionView  */
@property (strong, nonatomic) UITableView  *tableView;
/**  我的数据  */
@property (strong, nonatomic) LPZWYMyDetailModel  *detailModel;



@end

@implementation MyViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticeObject:) name:NOTI_UPDATE_TAB object:nil];
     self.leftButton.hidden = YES;
    [self layoutUI];
    
}

#pragma mark - 通知
- (void)noticeObject:(NSNotification *)noti{
    NSDictionary *dic = noti.object;
    if ([[dic objectForKey:@"index"] integerValue] == 3) {
        [self getData];
    }
}

- (void)getData{
    [self.tableView reloadData];
    [LPRequestManager request_getUserStudyTimeWithsuccessBlock:^(id object) {
        NSString *code = [object objectForKey:@"code"];
        if ([code integerValue] == 1) {
            self.detailModel = [LPZWYMyDetailModel yy_modelWithJSON:[object objectForKey:@"data"][0]];
            [self.tableView reloadData];
        }
        

    } failBlock:^(id object) {
       
    }];
}

#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.navigView , 0)
    .bottomSpaceToView(self.view , LP_TabBarHeight);
}



#pragma mark - LPZWYMYHeaderTableViewCellDelegate
- (void)LPZWYMYHeaderTableViewCell_clickHeadImage{
    __weak typeof(self) weakSelf = self;
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出当前用户" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginUser"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sessionId"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"username"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
        
        /**  这是退出登录  */
        
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[YBLoginViewController new]];
        UIWindow * window =  [[UIApplication sharedApplication].delegate window];
        window.rootViewController = nav;
        
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)LPZWYMYHeaderTableViewCell_clickIntoSettingView
{
    YBMySettingViewController * settingVC = [[YBMySettingViewController alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
}


- (void)LPZWYMYHeaderTableViewCell_clickFuncationViewWithTag:(NSInteger)tag{
    switch (tag) {
        case 0:
        {
            MyHeaderStudyAndClassViewController *vc = [MyHeaderStudyAndClassViewController new];
            vc.text = @"已获学时";
            vc.detailModel = self.detailModel;
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
        }
            break;
        case 1:
        {
            MyHeaderStudyAndClassViewController *vc = [MyHeaderStudyAndClassViewController new];
            vc.text = @"结业专题";
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            MyHeaderStudyAndClassViewController *vc = [MyHeaderStudyAndClassViewController new];
            vc.text = @"已学课程";
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 240 * [UIScreen zj_screenHScale];
    }
    return 240 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    if (indexPath.row == 0) {
        LPZWYMYHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[LPZWYMYHeaderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.delegate = self;
        cell.model = self.detailModel;
        return cell;
    }
    LPZWYmMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
    if (!cell) {
        cell = [[LPZWYmMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
    }
    cell.delegate = self;
     cell.model = self.detailModel;
    
    return cell;
}

#pragma mark - LPZWYmMyTableViewCellDelegate
- (void)LPZWYmMyTableViewCell_click_index:(NSInteger)index{
    switch (index) {
        case 0:
        {
            
            /**  我的专题  */
            ZJMySpecilViewController *vc = [ZJMySpecilViewController new];
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
            break;
        }
        case 1:
        {
            /**  我的课程  */
            ZJMyClassViewController *vc = [ZJMyClassViewController new];
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
            break;
        }
        case 2:
        {
            /**  我的收藏  */
            LPZWYMyCollectionViewController *vc = [LPZWYMyCollectionViewController new];
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
            break;
        }
            
        case 3:
        {
            /**  我的考试  */
            ZJWebViewController *vc = [[ZJWebViewController alloc]init];
            vc.isHiddenNav = YES;
            vc.titleString = @"我的考试";
            vc.urlString = [NSString stringWithFormat:@"%@html/indexfront.html#/myclassexamlist/%@/%@",HTMLURL,YBUSERINFOMODEL.userid,YBUSERINFOMODEL.studentId];
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
            break;
        }
        case 4:
        {
            /**  修改密码  */
            YBChangePassWordViewController *vc = [[YBChangePassWordViewController alloc]init];
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
            break;
        }

        default:
            break;
    }
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TabBarHeight - LP_NavViewHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
//        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 5 * [UIScreen zj_screenHScale])];
        
        
    }
    return _tableView;
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end
